import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

def perform_eda(file_path, event_name):
    """
    Perform Exploratory Data Analysis (EDA) on the given dataset.

    Parameters:
        file_path (str): Path to the dataset CSV file.
        event_name (str): Name of the event for labeling plots and outputs.
    """
    # Load the dataset
    data = pd.read_csv(file_path)
    
    # Convert 'Timestamp' to datetime format
    if 'Timestamp' in data.columns:
        data['Timestamp'] = pd.to_datetime(data['Timestamp'])
    
    # 1. Descriptive Statistics
    print(f"\n--- Descriptive Statistics for {event_name} ---\n")
    print(data.describe())

    # 2. Checking for Missing Values
    print(f"\n--- Missing Values in {event_name} ---\n")
    print(data.isnull().sum())

    # 3. Time Series Plot for Key Indicators
    if 'Timestamp' in data.columns:
        plt.figure(figsize=(12, 6))
        for column in ['Stock_Index', 'Bond_Yield_10Y_Treasury', 'Trading_Volume']:
            if column in data.columns:
                plt.plot(data['Timestamp'], data[column], label=column)
        plt.title(f"{event_name}: Time Series of Key Financial Indicators")
        plt.xlabel("Time")
        plt.ylabel("Value")
        plt.legend()
        plt.grid()
        plt.show()

    # 4. Correlation Heatmap
    plt.figure(figsize=(10, 8))
    correlation_matrix = data.corr()
    sns.heatmap(correlation_matrix, annot=True, fmt=".2f", cmap="coolwarm")
    plt.title(f"{event_name}: Correlation Heatmap")
    plt.show()

    # 5. Distribution of Key Variables
    key_columns = ['Stock_Index', 'Bond_Yield_10Y_Treasury', 'Trading_Volume']
    for column in key_columns:
        if column in data.columns:
            plt.figure(figsize=(8, 5))
            sns.histplot(data[column], kde=True, bins=30)
            plt.title(f"{event_name}: Distribution of {column}")
            plt.xlabel(column)
            plt.ylabel("Frequency")
            plt.grid()
            plt.show()

    # 6. Rolling Volatility Analysis (if exists)
    if 'Stock_Index_Volatility' in data.columns:
        plt.figure(figsize=(12, 6))
        plt.plot(data['Timestamp'], data['Stock_Index_Volatility'], label='Stock Index Volatility')
        plt.title(f"{event_name}: Rolling Volatility of Stock Index")
        plt.xlabel("Time")
        plt.ylabel("Volatility")
        plt.legend()
        plt.grid()
        plt.show()

    # 7. Interaction Term Analysis (if exists)
    if 'Shock_Stock_Interaction' in data.columns:
        plt.figure(figsize=(10, 6))
        sns.boxplot(x='Shock_Indicator', y='Shock_Stock_Interaction', data=data)
        plt.title(f"{event_name}: Shock Impact on Stock Interaction")
        plt.xlabel("Shock Indicator")
        plt.ylabel("Interaction Term")
        plt.grid()
        plt.show()

# File paths and event names
datasets = {
    "Global_Financial_Crisis": "C:\\Users\\HP\\Desktop\\processed_global_financial_crisis_features.csv",
    "Brexit": "C:\\Users\\HP\\Desktop\\processed_brexit_features.csv",
    "COVID_19_Crisis": "C:\\Users\\HP\\Desktop\\processed_covid_19_crisis_features.csv",
    "Eurozone_Debt_Crisis": "C:\\Users\\HP\\Desktop\\processed_eurozone_debt_crisis_features.csv",
    "Quantitative_Easing": "C:\\Users\\HP\\Desktop\\processed_quantitative_easing_features.csv",
    "Dodd_Frank_Act": "C:\\Users\\HP\\Desktop\\processed_dodd_frank_act_features.csv"
}

# Perform EDA for each dataset
for event_name, file_path in datasets.items():
    try:
        print(f"\n### Performing EDA for {event_name} ###\n")
        perform_eda(file_path, event_name)
    except FileNotFoundError:
        print(f"File not found: {file_path}. Please ensure the file exists.")
    except Exception as e:
        print(f"An error occurred while processing {event_name}: {e}")
